package com.tengfei.api.plan;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.course.domain.vo.CourseVo;
import com.tengfei.main.plan.domain.bo.PlanBo;
import com.tengfei.main.plan.domain.bo.PlanCoursesBo;
import com.tengfei.main.plan.domain.bo.PlanUsersBo;
import com.tengfei.main.plan.domain.vo.PlanCoursesVo;
import com.tengfei.main.plan.domain.vo.PlanSimpleVo;
import com.tengfei.main.plan.domain.vo.PlanUsersVo;
import com.tengfei.main.plan.domain.vo.PlanVo;
import com.tengfei.main.plan.service.IPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 学习计划Controller
 *
 * @author tengfei
 * @date 2022-08-01
 */
@Validated
@Api(value = "学习计划控制器", tags = {"学习计划管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/plan/plan")
public class PlanController extends BaseController {

    private final IPlanService iPlanService;

    /**
     * 查询学习计划课程列表
     */
    @ApiOperation("查询学习计划绑定课程列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/planCourse/list/bind")
    public R<List<PlanCoursesVo>> planCourseList(@ApiParam("学习计划id") @NotNull(message = "学习计划id不能为空")
                                                 @RequestParam("planId") Long planId) {
        return R.ok(iPlanService.queryPlanCourseList(planId));
    }

    /**
     * 查询学习计划未绑定课程列表
     */
    @ApiOperation("查询学习计划未绑定课程列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/planCourse/list/unBind")
    public TableDataInfo<CourseVo> planCourseListUnBind(@ApiParam("学习计划id") @NotNull(message = "学习计划id不能为空")
                                                        @RequestParam("planId") Long planId,
                                                        @ApiParam("课程名称")
                                                        @RequestParam(value = "courseName", required = false)
                                                                String courseName,
                                                        PageQuery pageQuery) {
        return iPlanService.queryPlanCourseListUnBind(planId, courseName, pageQuery);
    }

    /**
     * 查询学习计划用户列表
     */
    @ApiOperation("查询学习绑定计划用户列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/planUser/list/bind")
    public TableDataInfo<PlanUsersVo> planUserList(PlanUsersBo planUsersBo, PageQuery pageQuery) {
        return iPlanService.queryPlanUserList(planUsersBo, pageQuery);
    }

    /**
     * 查询学习计划未绑定用户列表
     */
    @ApiOperation("查询学习计划未绑定用户列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/planUser/list/unBind")
    public TableDataInfo<PlanUsersVo> planUserListUnBind(PlanUsersBo planUsersBo, PageQuery pageQuery) {
        return iPlanService.queryPlanUserListUnBind(planUsersBo, pageQuery);
    }


    /**
     * 批量删除学习计划课程
     */
    @ApiOperation("批量删除学习计划课程")
    @SaCheckPermission("plan:plan:remove")
    @Log(title = "批量删除学习计划课程", businessType = BusinessType.DELETE)
    @DeleteMapping("/planCourse")
    public R<Void> removePlanCourse(@ApiParam("id数组")
                                    @NotEmpty(message = "id数组不能为空")
                                    @RequestParam("ids") List<Long> ids) {
        return toAjax(iPlanService.deletePlanCourseByIds(ids) ? 1 : 0);
    }

    /**
     * 批量删除学习计划用户
     */
    @ApiOperation("批量删除学习计划用户")
    @SaCheckPermission("plan:plan:remove")
    @Log(title = "批量删除学习计划用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/planUser")
    public R<Void> removePlanUser(@ApiParam("id数组")
                                  @NotEmpty(message = "id数组不能为空")
                                  @RequestParam("ids") List<Long> ids) {
        return toAjax(iPlanService.deletePlanUserByIds(ids) ? 1 : 0);
    }

    /**
     * 批量新增学习计划课程
     */
    @ApiOperation("批量新增学习计划课程")
    @SaCheckPermission("plan:plan:add")
    @Log(title = "批量新增学习计划课程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/planCourse")
    public R<Void> addPlanCourse(@Validated(AddGroup.class) @RequestBody List<PlanCoursesBo> bos) {
        return toAjax(iPlanService.insertPlanCourseByBos(bos) ? 1 : 0);
    }

    /**
     * 批量新增学习计划用户
     */
    @ApiOperation("批量新增学习计划用户")
    @SaCheckPermission("plan:plan:add")
    @Log(title = "批量新增学习计划用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/planUser")
    public R<Void> addPlanUser(@Validated(AddGroup.class) @RequestBody List<PlanUsersBo> bos) {
        return toAjax(iPlanService.insertPlanUserByBos(bos) ? 1 : 0);
    }


    /**
     * 开启学习计划
     */
    @ApiOperation("开启学习计划")
    @SaCheckPermission("plan:plan:edit")
    @Log(title = "开启学习计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/open")
    public R<Void> openPlan(@ApiParam("学习计划id") @RequestParam("planId") Long id) {
        return toAjax(iPlanService.openPlan(id) ? 1 : 0);
    }

    /**
     * 关闭学习计划
     */
    @ApiOperation("关闭学习计划")
    @SaCheckPermission("plan:plan:edit")
    @Log(title = "关闭学习计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/close")
    public R<Void> clonePlan(@ApiParam("学习计划id") @RequestParam("planId") Long id) {
        return toAjax(iPlanService.closePlan(id) ? 1 : 0);
    }

    /**
     * 查询学习计划列表
     */
    @ApiOperation("查询学习计划列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/list")
    public TableDataInfo<PlanVo> list(PlanBo bo, PageQuery pageQuery) {
        return iPlanService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询学习简易计划列表
     */
    @ApiOperation("查询学习简易计划列表")
    @SaCheckPermission("plan:plan:list")
    @GetMapping("/list/simple")
    public R<List<PlanSimpleVo>> list(@ApiParam("计划名称") @RequestParam(value = "planName", required = false) String planName) {
        return R.ok(iPlanService.querySimpleList(planName));
    }

    /**
     * 获取学习计划详细信息
     */
    @ApiOperation("获取学习计划详细信息")
    @SaCheckPermission("plan:plan:query")
    @GetMapping("/{planId}")
    public R<PlanVo> getInfo(@ApiParam("学习计划id")
                             @NotNull(message = "学习计划id不能为空")
                             @PathVariable("planId") Long planId) {
        return R.ok(iPlanService.queryById(planId));
    }

    /**
     * 新增学习计划
     */
    @ApiOperation("新增学习计划")
    @SaCheckPermission("plan:plan:add")
    @Log(title = "新增学习计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PlanBo bo) {
        return toAjax(iPlanService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改学习计划
     */
    @ApiOperation("修改学习计划")
    @SaCheckPermission("plan:plan:edit")
    @Log(title = "修改学习计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PlanBo bo) {
        return toAjax(iPlanService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除学习计划
     */
    @ApiOperation("批量删除学习计划")
    @SaCheckPermission("plan:plan:remove")
    @Log(title = "批量删除学习计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("学习计划id数组")
                          @NotEmpty(message = "学习计划id数组不能为空")
                          @RequestParam("planIds") List<Long> ids) {
        return toAjax(iPlanService.deleteWithValidByIds(ids) ? 1 : 0);
    }
}
